package com.handmark.pulltorefresh.library;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

/**
 * 自动刷新的listView
 */
public class EndlessListView extends ListView implements OnScrollListener {

	boolean over_1_page_flag = true;
	float density = 2F;
	private PullLoadingListViewListener mLoadingListener;
	private EndlessListViewFooter mFooterView;
	private TextView endlessListview_text;
	private ImageView endlessListview_loadingAnimator;
	private int mTotalCount = 0;
	private Context context;
	private boolean enableLoading = true;// true 代表刷新 false 代表不刷新
	private boolean isLoading = false;
	private boolean allLoadingComplete = false;
	private OnScrollListener onScrollListener;

	private AnimationDrawable animaition;

	public EndlessListView(Context context) {
		super(context);
		initView(context);
	}

	public EndlessListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ListViewAttrs);
		enableLoading = a.getBoolean(R.styleable.ListViewAttrs_enableLoading, false);
		a.recycle();

		initView(context);
	}

	public EndlessListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initView(context);
	}

	private void initView(Context context) {
		this.context = context;

		density = context.getResources().getDisplayMetrics().density;

		// 判断是不是要刷新
		if (enableLoading == true) {
			setOnScrollListener(this);
		}

		mFooterView = new EndlessListViewFooter(context);
		endlessListview_text = (TextView) mFooterView
				.findViewById(R.id.endlessListview_text);
		endlessListview_loadingAnimator = (ImageView) mFooterView
				.findViewById(R.id.endlessListview_progressBar);
		startAnimation();
		addFooterView(mFooterView);
	}

	public void removeListener() {
		mLoadingListener = null;
	}

	public void setLoadingListener(PullLoadingListViewListener mLoadingListener) {
		this.mLoadingListener = mLoadingListener;
	}

	public void loadingCompleted() {
		mFooterView.setVisiableHeight(0);
		isLoading = false;
	}

	public void setIsLoading(boolean isLoading) {
		this.isLoading = isLoading;
	}

	public void setCustomOnScrollListener(OnScrollListener listener) {
		this.onScrollListener = listener;
	}

	public void allLoadingComplete() {
		allLoadingComplete = true;
		removeFooterView(mFooterView);
	}

	public void resetAllLoadingComplete() {
		removeFooterView(mFooterView);
		addFooterView(mFooterView);
		mFooterView.setVisibility(View.VISIBLE);
		endlessListview_loadingAnimator.setVisibility(View.VISIBLE);
		endlessListview_text.setText("加载中...");
		allLoadingComplete = false;
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
			int totalItemCount) {

		if (onScrollListener != null) {
			onScrollListener.onScroll(view, firstVisibleItem, visibleItemCount,
					totalItemCount);
		}

		mTotalCount = totalItemCount;
		over_1_page_flag = totalItemCount >= 10 ? true : false;
		if (over_1_page_flag && !allLoadingComplete) {
			if (mFooterView.getVisiableHeight() == 0) {
				mFooterView.setVisiableHeight((int) (80 * density));
			}
		}
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {

		InputMethodManager imm = (InputMethodManager) context
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(view.getWindowToken(), 0); // 强制隐藏键盘

		if (allLoadingComplete)
			return;

		if (scrollState == SCROLL_STATE_IDLE) {
			if (getLastVisiblePosition() >= mTotalCount - 2 && !allLoadingComplete) {
				if (mLoadingListener != null && isLoading == false && over_1_page_flag) {
					isLoading = true;
					mLoadingListener.onLoading();
					endlessListview_loadingAnimator.setVisibility(View.VISIBLE);
					endlessListview_text.setText("加载中...");
				}
			}
		}
	}

	public void startAnimation() {
		// if (!animaition.isRunning()) {
		// animaition.start();
		// } else {
		// animaition.stop();
		// animaition.start();
		// }
	}

	public interface PullLoadingListViewListener {

		public void onLoading();
	}
}
